package com.geekaca.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.geekaca.web.pojo.Brand;
import com.geekaca.web.pojo.BrandType;
import com.geekaca.web.service.BrandService;
import com.geekaca.web.utils.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/addBrand")
public class AddServlet extends HttpServlet {
    // 成员变量
    private BrandService brandService = new BrandService();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("Brand servlet...");

        ServletInputStream inputStream = req.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));

        String s = br.readLine();
        System.out.println(s);

//        String token = req.getHeader("token");
//        //判断token的有效性
//        if(token == null) {
//            //说明没有登录
//            return;
//        }
//        try{
//            Map<String, Claim> stringClaimMap = JwtUtil.verifyToken(token);
//            Claim id = stringClaimMap.get("id");
//            if(id==null){
//                // 说明token的格式不对，非法的访问
//
//            }
//        }catch (Exception ex){
//            ex.printStackTrace();
//            return;
//        }


        JSONObject jsonObject = JSON.parseObject(s, JSONObject.class);

        String brandName = (String) jsonObject.get("brandName");
        String companyName = (String) jsonObject.get("companyName");

        String status = (String) jsonObject.get("status");
        Integer statusInt = Integer.parseInt(status);

        String description = (String) jsonObject.get("description");

        String ordered = (String) jsonObject.get("ordered");
        Integer orderInt = Integer.valueOf(ordered);

        // 获取输入
//        String brandName = req.getParameter("brandName");
//        String companyName = req.getParameter("companyName");

//        String ordered = req.getParameter("ordered");
//        Integer orderInt = Integer.valueOf(ordered);

//        String brandTypeNo = req.getParameter("brandType");
//        Integer type_id = Integer.parseInt(brandTypeNo);

//        String description = req.getParameter("description");
//
//
//        String status = req.getParameter("status");
//        Integer statusInt = Integer.parseInt(status);

        StringBuilder msg = new StringBuilder();

        if (brandName == null || "".equals(brandName.trim())) {
            msg.append("品不能为空<br>");
        }

        if (companyName == null || "".equals(companyName.trim())) {
            msg.append("公司名称不能为空<br>");
        }
        if (!"".equals(msg.toString())) {
            String error = msg.toString();
            req.setAttribute("error", error);
            // 请求转发，浏览器地址栏不会变化
            req.getRequestDispatcher("/addBrand.jsp").forward(req, resp);
            return;
        }

        BrandType brandType;
        Integer type_id =1;

        switch (type_id) {
            case 1:
                brandType = new BrandType(1, 1, "食品");
                break;
            case 2:
                brandType = new BrandType(2, 2, "数码");
                break;
            case 3:
                brandType = new BrandType(3, 3, "文化");
                break;
            default:
                brandType = new BrandType();
        }


        Brand brand = new Brand(null, brandName, companyName, orderInt, description, statusInt,type_id, brandType);
        int added = brandService.addBrand(brand);
        Result result = new Result();
        resp.setHeader("Content-Type","text/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();


        if (added > 0) {
            //跳转到查询所有界面
            result.setCode(200);
            result.setMsg("新增成功");
            writer.write(JSON.toJSONString(result));
            writer.flush();

//            req.getRequestDispatcher("/brand.html").forward(req, resp);
        } else {
            result.setCode(400);
            result.setMsg("新增失败");
            String s1 = JSON.toJSONString(result);
            writer.write(s1);
            writer.flush();

        }


    }
}
